/*
 * Copyright (c) 2001, Aslak Hellesøy, BEKK Consulting
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * - Neither the name of BEKK Consulting nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */
package middlegen.plugins.entitybean;

import org.apache.log4j.Category;

import middlegen.javax.JavaTable;
import middlegen.RelationshipRole;
import middlegen.Table;

/**
 * Table decorator for the CMP30 plugin.
 *
 * @author <a href="mailto:eivind.waaler@bekk.no">Eivind Waaler</a>
 * @created 13. oktober 2004
 * @version $Id: CMP30Table.java,v 1.7 2005/11/13 18:19:30 eivindw Exp $
 */
public class CMP30Table extends JavaTable {

   /**
    * @todo-javadoc Describe the field
    */
   private String _pkGeneratorType;

   /**
    * @todo-javadoc Describe the field
    */
   private String _pkGeneratorName;

   /**
    * @todo-javadoc Describe the field
    */
   private static Category _log = Category.getInstance(CMP30Table.class.getName());

   /**
    * Constructor for the table decorator.
    *
    * @param subject The table being decorated.
    */
   public CMP30Table(Table subject) {
      super(subject);
   }

   /**
    * Sets the PkGeneratorType attribute of the CMP30Table object.
    *
    * @param pkGeneratorType The new PkGeneratorType value.
    */
   public void setPkGeneratorType(String pkGeneratorType) {
      setPrefsValue("pk-generator-type", pkGeneratorType);
      _pkGeneratorType = pkGeneratorType;
   }

   /**
    * Sets the PkGeneratorName attribute of the CMP30Table object.
    *
    * @param pkGeneratorName The new PkGeneratorName value.
    */
   public void setPkGeneratorName(String pkGeneratorName) {
      if (pkGeneratorName != null && pkGeneratorName.length() != 0) {
         setPrefsValue("pk-generator-name", pkGeneratorName);
      }
      _pkGeneratorName = pkGeneratorName;
   }

   /**
    * Check if the relationship is a one-to-one.
    *
    * @param relationshipRole The relationship role.
    * @return True if this relationship is one-to-one.
    */
   public boolean isOne2One(RelationshipRole relationshipRole) {
      return relationshipRole.getRelation().isOne2One();
   }


   /**
    * Check if the relationship is a one-to-many.
    *
    * @param relationshipRole The relationship role.
    * @return True if this relationship is one-to-many.
    */
   public boolean isOne2Many(RelationshipRole relationshipRole) {
      return relationshipRole.isTargetMany();
   }


   /**
    * Check if the relationship is a many-to-many.
    *
    * @param relationshipRole The relationship role.
    * @return True if this relationship is many-to-many.
    */
   public boolean isMany2Many(RelationshipRole relationshipRole) {
      return relationshipRole.getRelation().isMany2Many();
   }


   /**
    * Check if this table requires a composite key.
    *
    * @return True if the table requires a composite key.
    */
   public boolean isCompositeKey() {
      return getPkColumn() == null && !getPrimaryKeyColumns().isEmpty() ? true : false;
   }


   /**
    * Modify the class name to include generics if it's a collection.
    *
    * @param relationshipRole The relationship role.
    * @return The class name to represent the role.
    */
   public String getClassName(RelationshipRole relationshipRole) {
      String className = super.getClassName(relationshipRole);
      if (className.equals("java.util.Collection")) {
         JavaTable target = (JavaTable) relationshipRole.getTarget(getPlugin());
         className += "<" + target.getPackage() + "." + target.getDestinationClassName() + ">";
      }
      return className;
   }

   /**
    * Gets the PkGeneratorType attribute of the CMP30Table object.
    *
    * @return The PkGeneratorType value.
    */
   public String getPkGeneratorType() {
      return _pkGeneratorType;
   }

   /**
    * Gets the PkGeneratorName attribute of the CMP30Table object.
    *
    * @return The PkGeneratorName value.
    */
   public String getPkGeneratorName() {
      return _pkGeneratorName;
   }

   /**
    * Describe what the method does
    *
    * @todo-javadoc Write javadocs for method
    */
   protected void init() {
      super.init();
      String pkGeneratorType = getPrefsValue("pk-generator-type");
      if (pkGeneratorType != null) {
         setPkGeneratorType(pkGeneratorType);
         _log.debug("pk-generator-type from prefs (" + getSqlName() + "):" + getPkGeneratorType());
      }
      else {
         if (!isCompositeKey()) {
            setPkGeneratorType("AUTO");
            _log.debug("pk-generator-type from logic (" + getSqlName() + "):" + getPkGeneratorType());
         }
      }
      String pkGeneratorName = getPrefsValue("pk-generator-name");
      if (pkGeneratorName != null) {
         setPkGeneratorName(pkGeneratorName);
         _log.debug("pk-generator-name from prefs (" + getSqlName() + "):" + getPkGeneratorName());
      }
   }
}
